package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;

import com.thti.mecool.client.base.BaseActivity;

public class SettingActivity extends BaseActivity{
	ListView listView;
	HashMap<Integer, Boolean> items;
	String[] settings;
	SharedPreferences save;
	Editor editor;
	String[] names;
	 boolean frist=true;
	int index=-1;
	ArrayList<View> viewlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	noTitle();
	setContentView(R.layout.mecool_setting);
	names=new String[]{"BootLoad","AutoRefresh"};
save=getSharedPreferences("Setting", 0);
	viewlist=new ArrayList<View>();
	editor=save.edit();
	items=new HashMap<Integer, Boolean>();
	
	listView=(ListView)findViewById(R.id.mecool_setting_list);
	settings=new String[]{"启动程序加载数据","手动更新数据"};
	listView.setAdapter(ba);
	listView.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			CheckedTextView ct=(CheckedTextView)arg1;
				if(items.get(arg2)!=null){
					ct.setChecked(false);
				}else
					ct.setChecked(true);
			if(ct.isChecked()){
				items.put(arg2, true);
				ct.setCheckMarkDrawable(R.drawable.mecool_checkbox_click);
				editor.putBoolean(names[arg2], true);
			}else{
				ct.setCheckMarkDrawable(R.drawable.mecool_checkbox);
				editor.remove(names[arg2]);
				items.remove(arg2);
			}
			editor.commit();
			System.out.println(items);
		}
	}); 
}


private BaseAdapter ba=new BaseAdapter() {
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CheckedTextView v=  (CheckedTextView) getLayoutInflater().inflate(R.layout.mecool_setting_list_item, null);
		if(position==1||position==0)
			v.setEnabled(false);
		v.setText(settings[position]);
		if(save.getBoolean(names[position], false)){
			v.toggle();
			v.setCheckMarkDrawable(R.drawable.mecool_checkbox_click);
			items.put(position, true);
		}else{
			v.setCheckMarkDrawable(R.drawable.mecool_checkbox);
		}
		return v;
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public Object getItem(int position) {
		return settings[position];
	}
	
	@Override
	public int getCount() {
		return settings.length;
	}
};
}
